package tr.gov.tubitak.bilgem.esya.certviewer;

import javax.swing.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
import java.io.File;
import java.io.IOException;
import java.security.cert.CertificateParsingException;
import java.util.Locale;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 30.11.2011
 * Time: 11:34
 * To change this template use File | Settings | File Templates.
 */
public class EMainFrameController {
    private EMainFrameController(){};

    public static void main(String[] args) {
        setLookAndFeel();
        Locale.setDefault(new Locale("tr"));
        String filePath=null;


        if(args.length>0)
        {
            filePath=args[0];
        }
        else
        {
            JFileChooser openCertChooser = new JFileChooser();
            openCertChooser.setDialogTitle(Bundle.getString("EMainFrameController.SelectCertificate"));
            int retCode = openCertChooser.showOpenDialog(null);
            if(retCode != JFileChooser.ABORT)
            {
                File selectedFile = openCertChooser.getSelectedFile();
                filePath = selectedFile.getPath();
            }
        }
        final String finalFilePath = filePath;

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                // ECertViewerCertificate eCertificate = CardCertSelectDialog.selectCertificate();
                createView(finalFilePath);

            }
        });
    }
    private static void createView(final String certPath)
    {
        try {
            ECertViewerDialog.showCertificate(certPath);
        } catch (CertificateParsingException e) {
            JOptionPane.showMessageDialog(null,e.getMessage());
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null,e.getMessage());
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    private static void setLookAndFeel() {
        try {
            String osname = System.getProperty("os.name");
            if (osname.contains("nix") || osname.contains("nux"))
                UIManager.setLookAndFeel(new MetalLookAndFeel());
            else
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}